<?php
/**
 * author: JiangJun
 * CreateTime:2021/8/11 11:06 上午
 */

use Aws\Exception\AwsException;
use Aws\Sqs\SqsClient;

require './vendor/autoload.php';

$queueUrl = "https://sqs.us-west-2.amazonaws.com/863376839233/love";

$client = new SqsClient([
    'region' => 'us-west-2',
    'version' => '2012-11-05'
]);

try {
    //接收消息
    $result = $client->receiveMessage(array(
        'AttributeNames' => ['SentTimestamp'],
        'MaxNumberOfMessages' => 1,
        'MessageAttributeNames' => ['All'],
        'QueueUrl' => $queueUrl, // 只有它是必填的
        'WaitTimeSeconds' => 0,//等于0，就是短轮询
    ));
    if (!empty($result->get('Messages'))) {
        echo '取消息成功，消息体是：' . PHP_EOL ;
        //var_dump($result->get('Messages')[0]['Body']);
        $result['Messages'];
        echo '处理完成，准备删除...' . PHP_EOL;

        //删除消息
        $result = $client->deleteMessage([
            'QueueUrl' => $queueUrl, // REQUIRED
            //删除消息，必须要指定它的接收句柄(ReceiptHandle)。
            //接收句柄是和接收这个动作关联，而不是和消息本身关联。比如多次接收同一条消息，它的句柄是不同的
            'ReceiptHandle' => $result->get('Messages')[0]['ReceiptHandle'] // REQUIRED
        ]);
        print_r('---删除结果----' . PHP_EOL) ;
        var_dump($result);
    } else {
        echo "No messages in queue. \n";
    }
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}

